<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Controllers\Web\WebController;

use Config , Log , Validator , Cache;

use App\Libs\Sms;

class SendSmsController extends WebController
{

    public function checkCode(Request $request){
        $validation = Validator::make($request->all() , [
            'mobile'                  => 'required',
        ] , [
            'mobile.required'               => '没有填写手机号',
        ]);
        if($validation->fails()){
            $error = $validation->errors()->all();
            return response()->json(['code' => -1 , 'msg' => $error[0]]);
        }

        $mobile = $request->get('mobile');

        if(!isMobileNum($mobile)){
            return response()->json(['code' => -1 , 'msg' => '不支持该号码']);
        }

        $code = getSalt('6' , 1);

        $time = 1;

        Cache::put($mobile , $code , $time);

        Log::debug('验证码:'.$code);

        $res = Sms::sendSMS([
            'm' => $mobile ,
            'action' => 'sendParam' ,
            'c' => Config::get('sms.content.register') ,
            'p1' => $code ,
            'p2' => $time
        ]);

        Log::debug($res);
        return response()->json(['code' => 0 , 'msg' => '成功']);
    }
}
